package com.warm.pump.module.sys.controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.module.sys.bean.po.gen.DictGroup;
import com.warm.pump.module.sys.bean.po.gen.DictItem;
import com.warm.pump.module.sys.service.IDictGroupService;
import com.warm.pump.module.sys.service.IDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * sys_dict_item
 */
@Controller
@RequestMapping("/dictItem")
public class DictItemController {
    @Autowired
    private IDictItemService dictItemService;
    @Autowired
    private IDictGroupService dictGroupService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public OutJson list(Integer page, Integer limit, Long groupId) {
        Map<String, Object> map = new HashMap<>();
        map.put("groupId", groupId);
        map.put("orderBy", "order_num");
        //查询列表数据
        PageBean pageBean = dictItemService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/getDictItemByGroupCode")
    public OutJson getDictItemByGroupCode(String groupCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("groupCode", groupCode);
        DictGroup dictGroup = dictGroupService.queryObject(map);

        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("groupId", dictGroup.getId());
        itemMap.put("orderBy", "order_num");

        List<DictItem> dictItems = dictItemService.queryList(itemMap);

        return OutJson.ok().setResData(dictItems);
    }

    @ResponseBody
    @RequestMapping("/showDictItem")
    public OutJson showDictItem(String groupCode, String dictCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("groupCode", groupCode);
        DictGroup dictGroup = dictGroupService.queryObject(map);

        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("groupId", dictGroup.getId());
        itemMap.put("dictCode", dictCode);

        DictItem item = dictItemService.queryObject(itemMap);

        return OutJson.ok().setResData(item);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    public OutJson info(@PathVariable("id") Long id) {
        DictItem dictItem = dictItemService.queryObject(id);

        return OutJson.ok().put("dictItem", dictItem);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    public OutJson save(@RequestBody DictItem dictItem) {
        dictItem.setStatusDate(new Date());
        dictItemService.save(dictItem);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public OutJson update(@RequestBody DictItem dictItem) {
        dictItem.setStatusDate(new Date());
        dictItemService.update(dictItem);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public OutJson delete(@RequestBody Long[] ids) {
        dictItemService.deleteBatch(ids);

        return OutJson.ok();
    }

}
